package com.bawei.module_goods.adapter


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_goods.databinding.ItemMallBannerBinding
import com.bawei.module_goods.databinding.ItemMallEventBinding
import com.bawei.module_goods.databinding.ItemMallGoodsBinding
import com.bawei.module_goods.databinding.ItemMallTabBinding
import com.bawei.module_goods.section.GoodsSection


class GoodsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    companion object {
        const val TYPE_TAB = 0
        const val TYPE_BANNER = 1
        const val TYPE_EVENT = 2
        const val TYPE_GOODS = 3
    }

    var dataList: List<GoodsSection> = emptyList()
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun getItemViewType(position: Int): Int {
        return when (dataList[position]) {
            is GoodsSection.Tab -> TYPE_TAB
            is GoodsSection.Banner -> TYPE_BANNER
            is GoodsSection.Event -> TYPE_EVENT
            is GoodsSection.Goods -> TYPE_GOODS
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_TAB -> TabViewHolder(ItemMallTabBinding.inflate(LayoutInflater.from(parent.context), parent, false))
            TYPE_BANNER -> BannerViewHolder(ItemMallBannerBinding.inflate(LayoutInflater.from(parent.context), parent, false))
            TYPE_EVENT -> EventViewHolder(ItemMallEventBinding.inflate(LayoutInflater.from(parent.context), parent, false))
            TYPE_GOODS -> GoodsViewHolder(ItemMallGoodsBinding.inflate(LayoutInflater.from(parent.context), parent, false))
            else -> throw IllegalArgumentException("Invalid viewType")
        }
    }

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val section = dataList[position]) {
            is GoodsSection.Tab -> (holder as TabViewHolder).bind(section)
            is GoodsSection.Banner -> (holder as BannerViewHolder).bind(section)
            is GoodsSection.Event -> (holder as EventViewHolder).bind(section)
            is GoodsSection.Goods -> (holder as GoodsViewHolder).bind(section)
        }
    }

    class BannerViewHolder(private val binding: ItemMallBannerBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(section: GoodsSection.Banner) {
            val banners = section.banners
            binding.ivBanner.setAdapter(BannerAdapter(banners))
            binding.ivBanner.isAutoLoop(true)
            binding.ivBanner.start()
        }
    }

    class TabViewHolder(private val binding: ItemMallTabBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(section: GoodsSection.Tab) {
            val tabAdapter = MallTabAdapter()
            binding.recyclerViewTabs.layoutManager = LinearLayoutManager(binding.root.context, LinearLayoutManager.HORIZONTAL, false)
            binding.recyclerViewTabs.adapter = tabAdapter
            tabAdapter.submitList(section.tabs)
        }
    }

    class EventViewHolder(private val binding: ItemMallEventBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(section: GoodsSection.Event) {
            val eventAdapter = MallEventAdapter()
            binding.recyclerViewEvents.layoutManager = LinearLayoutManager(binding.root.context, LinearLayoutManager.HORIZONTAL, false)
            binding.recyclerViewEvents.adapter = eventAdapter
            eventAdapter.submitList(section.events)
        }
    }
    class GoodsViewHolder(private val binding: ItemMallGoodsBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(section: GoodsSection.Goods) {
            val eventAdapter = MallGoodsAdapter()
            binding.recyclerViewInner.layoutManager = LinearLayoutManager(binding.root.context)
            binding.recyclerViewInner.adapter = eventAdapter
            eventAdapter.submitList(section.events)
        }
    }
}